﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using OutdoorLib.BaseEntity.Face;


public partial class Ctl_ctlContactFace : System.Web.UI.UserControl
{
    
    

    //private Func<bool> _OnButtonClick;
    //private Func<bool> _OnMyDataChanged;
    //private Func<bool> _OnBeforeMyDataDataChanged;
    ///// <summary>
    ///// События - Кнопка Save|Cancel
    ///// </summary>
    //public Func<bool> OnButtonClick { get { return _OnButtonClick; } set { _OnButtonClick = value; } }
    //public Func<bool> OnMyDataChanged { get { return _OnMyDataChanged; } set { _OnMyDataChanged = value; } }
    //public Func<bool> OnBeforeMyDataDataChanged { get { return _OnBeforeMyDataDataChanged; } set { _OnBeforeMyDataDataChanged = value; } }

    public string FaceID { get { return hfContactFaceId.Value; } set { hfContactFaceId.Value = value; } }
    
    public  Func<string, ContactFace> FaceSource= null;
    public Func<bool> OnCancelClick = null;
    public Func<bool> OnSaveClick = null;
    protected  ContactFace ContactFace
    {
        get
        {
            if (null != FaceSource)
                return FaceSource(FaceID);
            return null;
            
        }
    }

    

     protected override void OnDataBinding(EventArgs e)
     {
             base.OnDataBinding(e);

             lvContactItems.DataSource = ContactItems(ContactFace);
             lvContactItems.DataBind();

     }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {

    }
    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    {

    }
    protected void btnRemoveContactItem_Click(object sender, ImageClickEventArgs e)
    {                
        ContactFace.ContactList.Remove(((ImageButton)sender).CommandArgument);
        lvContactItems.DataSource = ContactItems(ContactFace);
        lvContactItems.DataBind();        

        
    }
    protected void btnAddContactItem_Click(object sender, ImageClickEventArgs e)
    {
        Contact NewC = new Contact();
        NewC.ContactValue = txtContactValue.Text  ;
        NewC.ContactType = Contact.ConvertStringToContactType ( rbContactType.SelectedItem.Value);      
        NewC.MainContact = chMainContact.Checked;        
        ContactFace.ContactList.Add(NewC);
        lvContactItems.DataSource = ContactItems(ContactFace);
        lvContactItems.DataBind();
        txtContactValue.Text = string.Empty;
        
        ReadForm();
        AppEnvironment.ObjectFactory.SaveObject(ContactFace);

    }

    protected Contact[] ContactItems(ContactFace Worker)
    {
        Contact[] c = Worker.ContactList.GetSortedList();
        return c;
    }

    protected void ReadForm()
    {
        ContactFace.FirstName = txtFirstName.Text;
        ContactFace.SecondName = txtSecondName.Text;
        ContactFace.LastName = txtLastName.Text;
        ContactFace.Position = txtPosition.Text;
    }

    protected void btnSaveWorker_Click(object sender, EventArgs e)
    {
        ReadForm();
        AppEnvironment.ObjectFactory.SaveObject(ContactFace);
        if (null != OnSaveClick)
                        OnSaveClick();

    }

    protected string ClientName 
    { 
        get 
        {
            return string.Empty;
            //wsFace ws = (wsFace)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
            //Client cl= ws.GetClient (ContactFace .ClientID );
            //return cl.FullName;
        } 
    }
}
